package decisiontree;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

public class DecisionTree<Email> {
	
	protected NoTree<Email> root; //Tree root
	
	public DecisionTree(Hashtable<String, Integer> characteristcUniversal, Vector<Vector<Integer>> characteristicpositive, Vector<Vector<Integer>> characteristicnegative){
		root = new NoTree<Email>(characteristcUniversal, characteristicpositive, characteristicnegative);
	}
	
	public void calculate() throws Exception{
		root.analise("No");
	}
	
	public String getFilter(){
		return root.getFilter();
	}
	
	public String toString(){
		return root.toString(1);
	}
	
	public static double information(double p, double n){
		
		if(p == 0 && n == 0)
			return 0;
		else if(p == 0)
			return -(n/(p+n))*log2(n/(p+n));
		else if(n == 0)
			return -(p/(p+n))*log2(p/(p+n));
		else
			return -(p/(p+n))*log2(p/(p+n)) -(n/(p+n))*log2(n/(p+n));
	}
	
	public static double remainder(double p0, double n0, double p1, double n1){	
		double part1 = (p0+n0)/(p0+p1 + n0 + n1)*information(p0, n0);
		double part2 = (p1+n1)/(p0+p1 + n0 + n1)*information(p1, n1);
		return part1 + part2;
	}
	
	public static double gain(double p0, double n0, double p1, double n1){
		return information(p0 + p1, n0 + n1) - remainder(p0, n0, p1, n1);
	}
	
	public static double log2(double x){
		return Math.log(x)/Math.log(2);
	}
	
}
